package com.xiaolin.io;

import java.io.*;

/**
 * @ClassName TestInputStreamReader
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/25
 * @Version 1.0
 */
public class TestInputStreamReader {
    public static void main(String[] args) throws IOException {
        boolean b = readAndWrite("D:\\1file\\hello.rtf", "D:\\1file\\out.rtf");
        System.out.println(b);
    }

    /**
     * 读写纯字符文件，有中文需要指定为GBK，不能指定为utf-8，该方法可以转化包含中文的.txt和.rtf等类型的文件，但是转化doc、docx等文件类型的会乱码
     * @param origin
     * @param dest
     * @return
     */
    public static boolean readAndWrite(String origin,String dest){
        InputStreamReader reader= null;
        OutputStreamWriter writer = null;
        try {
            reader = new InputStreamReader(new FileInputStream(origin),"GBK");
            writer=new OutputStreamWriter(new FileOutputStream(dest),"GBK");
            char[] chars=new char[1024*5];  //代表1024*5个字符
            int read=0;
            while ((read=reader.read(chars)) != -1){
                writer.write(chars,0,read);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}
